function ProductDetailAssistant(argFromPusher) {
	this.nid = argFromPusher.nid;
}

ProductDetailAssistant.prototype = {
	product: null,
	attributes: [],
	setup: function() {
		Ares.setupSceneAssistant(this);
		this.$.priceRow.setShowing(showPrice(user));
		var this1 = this;
		if(offline){
			db.transaction( function(transaction) {
				transaction.executeSql( "SELECT distinct nid, title, body, version, vendor, price, voting FROM products WHERE nid=? ", [this1.nid],
					function (transaction, result) {
						this1.product = result.rows.item(0);
						var p = this1.product;
						this1.$.titleLbl.setLabel(p.title);
						this1.$.priceLbl.setLabel("$"+parseFloat(p.price).toFixed(2));
						this1.$.versionLbl.setLabel(p.version);
						this1.$.vendorLbl.setLabel(p.vendor);
						this1.$.ratingLbl.setLabel((p.voting/20).toFixed(1) + " out of 5");
						this1.$.cartGrp.setShowing(false);
						this1.$.eduAttrGrp.setShowing(false);
						this1.$.ratingGrp.setShowing(false);
						this1.$.commentGrp.setShowing(false);
						this1.$.relatedGrp.setShowing(false);
						this1.controller.get('scroller1').mojo.revealTop();
					},
					errorHandler.bind(this1));
			});
		}else{
			this.getDetail();
			this.getRating();
		}
	},
	cleanup: function() {
		Ares.cleanupSceneAssistant(this);
	},
	activate: function(event) {
		if(!event || !event.attr) {
			this.controller.get('scroller1').mojo.revealTop();
			return;
		}
		this.attributes.each(function(attr){
			if(attr.aid == event.attr.aid)
				attr.choice = event.choice.name;
				if(!attr.choice) attr.choice = "PICK ONE";
		});
		this.$.attrList.setItems(this.attributes);
	},
	button2Tap: function(inSender, event) {
		var validate = true;
		var this1 = this;
		this.attributes.each(function(attr){
			if(!attr.choice || attr.choice == "PICK ONE"){
				myAlert(this1, "Please select " + attr.name);
				validate = false;
				throw $break;
			}
		});
		if(!validate) return;
		var attrStr = "";
		this.product.data = {attributes: {}, shippable: "", module: "uc_product"};
		this.attributes.each(function(a){
			attrStr += a.name + ": " + a.choice + "\n";
			this1.product.data.attributes[a.name] = new Array(a.choice);
		});
		addToCart({
			nid: this.product.nid, 
			title: this.product.title, 
			model: this.product.model, 
			price: this.product.sell_price,
			data: this.product.data,
			attrStr: attrStr
		});
		myAlert(this, this.product.title + " is added to cart.", function(){
			this1.controller.get('scroller1').mojo.revealTop();
		},null, null, null, true);
	},
	getDetail: function(){
		var this1 = this;
		drupalService('node.get', {nid: this.nid}, this, function(data) {
			this1.product = data;
			//this1.$.header.setLabel(data.title);
			this1.$.titleLbl.setLabel(data.title);
			this1.$.priceLbl.setLabel("$"+parseFloat(data.sell_price).toFixed(2));
			var relatedItems = [];
			if(data.field_relateditem){
				Object.keys(data.field_relateditem.items).each(function(key){
					relatedItems.push(data.field_relateditem.items[key]);
				});
				this1.$.relatedList.setItems(relatedItems);
				this1.$.relatedList.setHeight(relatedItems.length * 56);
				this1.$.relatedListRow.setHeight(this1.$.relatedList.getHeight());
			}
			if(data.type == "edicationalitem"){
				this1.$.attrListRow.setShowing(false);
				this1.$.versionRow.setShowing(false);
				this1.$.vendorRow.setShowing(false);
				this1.$.requestBtn.setShowing(false);
				var a;
				a = deliveryTypes.filter(function(v,k){
					return data.field_deliverytype.containInField("value", k);
				});
				this1.$.deliveryTypeLbl.setLabel(a.join("\n"));
				a = courseLevels.filter(function(v,k){
					return data.field_courselevel.containInField("value", k);
				});
				this1.$.courseLevelLbl.setLabel(a.join("\n"));
				this1.$.courseDurationLbl.setLabel(data.field_courseduration[0].value + "DAYS");
			}else{
				this1.$.eduAttrGrp.setShowing(false);
				this1.$.versionLbl.setLabel(data.field_softwareversion[0].value);
				this1.$.vendorLbl.setLabel(data.field_vendor_name[0].value);
				this1.attributes = [];
				Object.keys(data.attributes).each(function(key){
					var attr = data.attributes[key];
					var options = [];
					Object.keys(attr.options).each(function(k){
						options.push(attr.options[k]);
					});
					attr.options = options;
					attr.choice = "PICK ONE";
					this1.attributes.push(attr);
				});
				this1.$.attrList.setItems(this1.attributes);
				this1.$.attrList.setHeight(56 * this1.attributes.length);
				this1.$.attrListRow.setHeight(this1.$.attrList.getHeight());
			}
		});
	},
	getRating: function(){
		var this1 = this;
		var args = { content_type: "node", content_id: this.nid, uid: user.uid };
		drupalService('votingapi.getUserVotes', args, this, function(data) {
			if(data)
				this1.$.ratingSel.setValue(data.value);
		});
		args = { content_type: "node", content_id: this.nid }; 
		drupalService('votingapi.getVotingResults', args, this, function(data) {
			if(data)
				this1.$.ratingLbl.setLabel((data.value/20).toFixed(1) + " out of 5");
		});
	},
	button3Tap: function(inSender, event) {
		Mojo.Controller.stageController.pushScene("productComment",{product: this.product});
	},
	button4Tap: function(inSender, event) {
		var args = {
			comment: {
				nid: this.product.nid, 
				comment: this.$.commentBox.getValue(),
				name: user.name
			}
		};
		var this1 = this;
		drupalService('comment.save', args, this, function(data) {
			myAlert(this1, "Comment is submitted", function(){
				this1.$.commentBox.setValue("");
			}, null, null, null, true);
		});
	},
	relatedListListtap: function(inSender, event) {
		Mojo.Controller.stageController.popScene();
		Mojo.Controller.stageController.pushScene("productDetail",{nid: event.item.nid});
	},
	attrListListtap: function(inSender, event) {
		Mojo.Controller.stageController.pushScene("AttributeList", {attribute: event.item});
	},
	rateBtTap: function(inSender, event) {
		var this1 = this;
		var args = { 
				content_type: "node", 
				content_id: this.nid, 
				vote: this.$.ratingSel.getValue(),
				uid: user.uid
		};
		drupalService('votingapi.setVote', args, this, function(data) {
			if(data)
				myAlert(this1, "Your rating is submitted.", this1.getRating.bind(this1), null, null, null, true);
		});
	},
	descLblTap: function(inSender, event) {
		var desc = this.product.body.replace(/width\s*:\s*\d+px\s*;/, "").replace(/\[flashnode\|nid=\d+\]/, "");
		Mojo.Controller.stageController.pushScene("staticHtml", 
				{title: this.product.title, body: desc});
	},
	requestBtnTap: function(inSender, event) {
		var desc = "Request new package\n";
		this.attributes.each(function(a){
			desc += a.name + ": \n";
		});
		var form = {
			catyLs : 1,
			titleBox : this.product.title,
			descBox : desc
		};
		Mojo.Controller.stageController.pushScene("newRequest", {form: form});
	},
};